package com.example.houduan.Controller;

import com.example.houduan.Service.AccountService;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;
import java.util.concurrent.TimeUnit;


@Controller
@CrossOrigin
public class LoginController {
    @Autowired
    RedisTemplate<Object,Object> rt;

    @Autowired
    AccountService accountService;

    @RequestMapping("/getUname")
    @ResponseBody
    public String getUname() {
        Object uname = rt.opsForValue().get("uname");
        return uname != null ? uname.toString() : "未登录用户";
    }

    @RequestMapping("/yzm")
    public void yzm(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {
        resp.setContentType("image/jpeg");
        BufferedImage image = new BufferedImage(80,30,BufferedImage.TYPE_INT_RGB);
        Color[] colors={Color.RED,Color.BLUE,Color.GREEN};
        Graphics g=image.getGraphics();
        g.setColor(Color.WHITE);
        g.fillRect(0,0,image.getWidth(),image.getHeight());
        String strs="ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
        StringBuffer sb=new StringBuffer();
        for (int i = 0; i < 4; i++) {
            char str=strs.charAt(new Random().nextInt(strs.length()));
            sb.append(str);
            g.setColor(colors[new Random().nextInt(colors.length)]);
            g.setFont(new Font("宋体",Font.BOLD,22));
            g.drawString(String.valueOf(str),i*20,20);
        }

        for (int i = 0; i < 10; i++) {
            g.setColor(colors[new Random().nextInt(colors.length)]);
            g.drawLine(new Random().nextInt(80),new Random().nextInt(30),new Random().nextInt(80),new Random().nextInt(30));
        }
        rt.opsForValue().set("yzm",sb.toString(),30, TimeUnit.MINUTES);
        OutputStream out = resp.getOutputStream();
        ImageIO.write(image,"jpg",out);
        out.flush();
        out.close();

    }

    @RequestMapping("/login")
    @ResponseBody
    public String Login(@RequestParam String uname, String pwd, String yzm){
        String realYzm=rt.opsForValue().get("yzm").toString();
        if(realYzm.equalsIgnoreCase(yzm)){
            if(accountService.getAccountByUnameAndPwd(uname,pwd)!=null){
                rt.opsForValue().set("uname", uname, 30, java.util.concurrent.TimeUnit.MINUTES);
                return "yes";
            }else {
                return "用户名或密码输入错误";
            }
        }else {
            return "验证码输入错误";
        }
    }
}
